package com.wisemoresystem.seal.action.mac.destory;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.MacMoveLogService;
import com.wisemoresystem.seal.service.MacService;

/**
 * 报废
 * 
 * @author 冯晓东
 *
 */
@Controller("macDestory")
@Scope("prototype")
@RequestMapping("/ix/mac/destory")
public class MacDestoryAction extends ProjBaseAction {

	@Autowired
	private MacService macService;
	@Autowired
	private MacMoveLogService macLogService;

	/**
	 * 印控仪报废列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> macList = macService.selectStopingList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/mac/destory/list");
		mv.addObject("macList", macList);
		mv.addObject("srh_name", srh_name);
		return mv;
	}

	/**
	 * 转向报废页面
	 * 
	 * @param mac_id
	 *            印控仪id
	 * @return
	 */
	@RequestMapping("/destory_form")
	public ModelAndView destory_form(String mac_id) {

		t_mac mac = oc.findObjById(mac_id, t_mac.class);
		t_user user = oc.findObjById(mac.getUser_id(), t_user.class);
		t_bank bank = oc.findObjById(mac.getUser_bank_id(), t_bank.class);

		String userName = user.getUname();
		String userCode = user.getCode();
		String bankName = bank.getName();
		String bankCode = bank.getCode();

		ModelAndView mv = new ModelAndView("/jsp/mac/destory/destory_form");
		mv.addObject("mac", mac);
		mv.addObject("userName", userName);
		mv.addObject("userCode", userCode);
		mv.addObject("bankName", bankName);
		mv.addObject("bankCode", bankCode);
		return mv;
	}

	/**
	 * 印控仪报废
	 * 
	 * @param mac_id
	 *            印控仪报废id
	 * @return
	 */
	@RequestMapping("/destory")
	public ModelAndView destory(String mac_id) {

		boolean isStart = macLogService.destory(mac_id);
		if (isStart) {
			this.setTipMsg("报废成功!", Tip.Type.success);
			logT.logH("印控仪报废", t_mac.class, mac_id);
		} else {
			this.setTipMsg("报废失败!该设备状态有误!", Tip.Type.error);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 印控仪批量报废
	 * 
	 * @param check_ids
	 *            所有报废信息的id的字符串
	 * @return
	 */
	@RequestMapping("/verify_all")
	public ModelAndView verify_all(String check_ids) {

		String[] ids = check_ids.split(",");
		for (String mac_id : ids) {
			if (ChkTools.isNotNull(mac_id)) {
				boolean isStart = macLogService.destory(mac_id);
				if (isStart) {
					logT.logH("印控仪报废", t_mac.class, mac_id);
				}
			}
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

}
